package NewCoder;

public class BM13 {
    public boolean isPail (ListNode head) {
        // write code here
        if(head==null || head.next==null) return true;
        ListNode slow=head;
        ListNode fast=head.next;
        while(fast!=null && fast.next!=null){
            slow=slow.next;
            fast=fast.next.next;
        }
        ListNode tmp=slow.next;
        slow.next=null;

        ListNode p=reverse(tmp);
        ListNode q=head;
        while(p!=null){
            if(p.val!=q.val){
                return false;
            }
            p=p.next;
            q=q.next;
        }
        return true;
    }
    private ListNode reverse(ListNode node){
        if(node==null) return null;
        ListNode cur=node;
        ListNode pre=null;
        while(cur!=null){
            ListNode next=cur.next;
            cur.next=pre;
            pre=cur;
            cur=next;
        }
        return pre;
    }
}
